<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="设计模式,">





  <link rel="alternate" href="/blog/atom.xml" title="编程那点事" type="application/atom+xml">






<meta name="description" content="看看用TypeScript怎样实现常见的设计模式，顺便复习一下。学模式最重要的不是记UML，而是知道什么模式可以解决什么样的问题，在做项目时碰到问题可以想到用哪个模式可以解决，UML忘了可以查，思想记住就好。这里尽量用原创的，实际中能碰到的例子来说明模式的特点和用处。 策略模式 Strategy特点：用组合的方式调用一些算法或逻辑，并且可以根据状态不同而选用不同的算法或逻辑。用处：对象需要运行时切">
<meta name="keywords" content="设计模式">
<meta property="og:type" content="article">
<meta property="og:title" content="TypeScript设计模式之策略、模板方法">
<meta property="og:url" content="https://lhweb.gitee.io/blog/2018/09/23/TypeScript设计模式之策略、模板方法/index.html">
<meta property="og:site_name" content="编程那点事">
<meta property="og:description" content="看看用TypeScript怎样实现常见的设计模式，顺便复习一下。学模式最重要的不是记UML，而是知道什么模式可以解决什么样的问题，在做项目时碰到问题可以想到用哪个模式可以解决，UML忘了可以查，思想记住就好。这里尽量用原创的，实际中能碰到的例子来说明模式的特点和用处。 策略模式 Strategy特点：用组合的方式调用一些算法或逻辑，并且可以根据状态不同而选用不同的算法或逻辑。用处：对象需要运行时切">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-10-03T05:53:45.929Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="TypeScript设计模式之策略、模板方法">
<meta name="twitter:description" content="看看用TypeScript怎样实现常见的设计模式，顺便复习一下。学模式最重要的不是记UML，而是知道什么模式可以解决什么样的问题，在做项目时碰到问题可以想到用哪个模式可以解决，UML忘了可以查，思想记住就好。这里尽量用原创的，实际中能碰到的例子来说明模式的特点和用处。 策略模式 Strategy特点：用组合的方式调用一些算法或逻辑，并且可以根据状态不同而选用不同的算法或逻辑。用处：对象需要运行时切">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lhweb.gitee.io/blog/2018/09/23/TypeScript设计模式之策略、模板方法/">





  <title>TypeScript设计模式之策略、模板方法 | 编程那点事</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">编程那点事</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://lhweb.gitee.io/blog/blog/2018/09/23/TypeScript设计模式之策略、模板方法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lhweb">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="编程那点事">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">TypeScript设计模式之策略、模板方法</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-09-23T21:49:54+08:00">
                2018-09-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/typescript/" itemprop="url" rel="index">
                    <span itemprop="name">typescript</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>看看用TypeScript怎样实现常见的设计模式，顺便复习一下。<br>学模式最重要的不是记UML，而是知道什么模式可以解决什么样的问题，在做项目时碰到问题可以想到用哪个模式可以解决，UML忘了可以查，思想记住就好。<br>这里尽量用原创的，实际中能碰到的例子来说明模式的特点和用处。</p>
<h1 id="策略模式-Strategy"><a href="#策略模式-Strategy" class="headerlink" title="策略模式 Strategy"></a>策略模式 Strategy</h1><p>特点：用组合的方式调用一些算法或逻辑，并且可以根据状态不同而选用不同的算法或逻辑。<br>用处：对象需要运行时切换算法或逻辑可以考虑使用策略模式。<br>注意：策略的生成方式。<br>下面用TypeScript简单实现一个策略模式：<br>说起策略就想到策略类游戏，年龄大点的可能都玩过War3，人族对兽族时如果侦察到对方不着急升本，用常规万金油打法，那人族就可以出狗男女来一波流。<br>如果侦察到兽族跳科技并摆下两个兽栏，那对方可能是暴飞龙，人族就要家里补个塔防偷农民，然后出点火枪或二本龙鹰。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Orc</span></span>&#123;</span><br><span class="line">    private _shenKeJi = <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">get</span> shenKeJi(): boolean &#123; <span class="comment">// 这里简单用升科技来判断是用常规还是飞龙</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>._shenKeJi;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">set</span> shenKeJi(value: boolean)&#123;</span><br><span class="line">        <span class="keyword">this</span>._shenKeJi = value;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">abstract <span class="class"><span class="keyword">class</span> <span class="title">Stragety</span></span>&#123;</span><br><span class="line">    abstract execute();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RushStragety</span> <span class="keyword">extends</span> <span class="title">Stragety</span></span>&#123;</span><br><span class="line">    execute()&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'升科技'</span>);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'出狗男女'</span>);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'一波流'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DefendStragety</span> <span class="keyword">extends</span> <span class="title">Stragety</span></span>&#123;</span><br><span class="line">    execute()&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'补塔防飞龙'</span>);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'出火枪'</span>);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'升科技'</span>);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'出龙鹰'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Human</span></span>&#123;</span><br><span class="line">    stragety: Stragety;</span><br><span class="line"></span><br><span class="line">    checkOrc(orc: Orc)&#123;</span><br><span class="line">        <span class="keyword">if</span>(orc.shenKeJi)&#123; <span class="comment">//根据兽族情况来决定策略</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'侦察到兽族是跳科技打法'</span>);</span><br><span class="line">            <span class="keyword">this</span>.stragety = <span class="keyword">new</span> DefendStragety();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'侦察到兽族是常规打法'</span>);</span><br><span class="line">            <span class="keyword">this</span>.stragety = <span class="keyword">new</span> RushStragety();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    deal()&#123;</span><br><span class="line">        <span class="keyword">this</span>.stragety &amp;&amp; <span class="keyword">this</span>.stragety.execute();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> orc = <span class="keyword">new</span> Orc();</span><br><span class="line"><span class="keyword">let</span> human = <span class="keyword">new</span> Human();</span><br><span class="line"></span><br><span class="line">orc.shenKeJi = <span class="literal">false</span>;</span><br><span class="line">human.checkOrc(orc);</span><br><span class="line">human.deal();</span><br><span class="line"></span><br><span class="line">orc.shenKeJi = <span class="literal">true</span>;</span><br><span class="line">human.checkOrc(orc);</span><br><span class="line">human.deal();</span><br><span class="line"></span><br><span class="line"><span class="comment">//输出</span></span><br><span class="line">侦察到兽族是常规打法</span><br><span class="line">升科技</span><br><span class="line">出狗男女</span><br><span class="line">一波流</span><br><span class="line"></span><br><span class="line">侦察到兽族是跳科技打法</span><br><span class="line">补塔防飞龙</span><br><span class="line">出火枪</span><br><span class="line">升科技</span><br><span class="line">出龙鹰</span><br></pre></td></tr></table></figure></p>
<p>这样人族就可以根据兽族的状态改变来做出不同的应对策略，其实现在游戏的AI基本都是通过决策树来实现的，也算是策略模式，只是更复杂，通过各种不同的条件最终得到一个决策来做出反应。</p>
<p>另外，有人可能已经发现了，上面生成策略的地方是可以拿出来，用之前讲的工厂模式来做，因为实际应用时策略通常比较多，甚至可能同时需要多种相关策略，用工厂模式来生产策略就可以很好的隐藏细节，解除依赖。</p>
<h1 id="模板方法模式-Template-Method"><a href="#模板方法模式-Template-Method" class="headerlink" title="模板方法模式 Template Method"></a>模板方法模式 Template Method</h1><p>特点：通过多态来实现在运行时使用不同的算法或逻辑，通常有一个整体架子，通过抽象方法或虚方法来把细节代码延迟到子类实现。<br>用处：当多个类似功能的类有很多相同结构或代码时，可以抽象出整体架子时可以考虑模板方法。<br>注意：与策略模式的异同：同样是细节部分交出去，不同在于策略是对象行为，采用的是组合的方式，而模板方法是类行为，采用的是继承。<br>下面用TypeScript简单实现一个模板方法模式：<br>比方说发送http请求的代码，需要向两台不同的server(A和B)发送请求，两台server除了url不同，回来的数据格式也不一样，但由于都是http请求，主体架子是一样的，所以可以用模板方法来实现下。<br><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ClassA</span></span>&#123;&#125; <span class="comment">// Server A 返回的数据结构</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ClassB</span></span>&#123;&#125; <span class="comment">// Server B 返回的数据结构</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">RequesterBase&lt;T&gt;</span></span>&#123;</span><br><span class="line"></span><br><span class="line">    constructor(<span class="keyword">private</span> url: string)&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    reqeustData(): <span class="type">T</span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.sendReqeust();</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.handleResponse();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> sendReqeust()&#123;</span><br><span class="line">        console.log(`send request, url: $&#123;<span class="keyword">this</span>.url&#125;`);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">abstract</span> handleResponse(): <span class="type">T</span>; <span class="comment">// 不同的server返回的数据交由子类去实现</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RequesterForServerA</span> <span class="keyword">extends</span> <span class="title">RequesterBase&lt;ClassA&gt;</span></span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> handleResponse(): <span class="type">ClassA</span>&#123;</span><br><span class="line">        console.log(<span class="symbol">'handle</span> response <span class="keyword">for</span> <span class="type">Server</span> <span class="type">A</span>');</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RequesterForServerB</span> <span class="keyword">extends</span> <span class="title">RequesterBase&lt;ClassB&gt;</span></span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> handleResponse(): <span class="type">ClassB</span>&#123;</span><br><span class="line">        console.log(<span class="symbol">'handle</span> response <span class="keyword">for</span> <span class="type">Server</span> <span class="type">B</span>');</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">let requesterA: <span class="type">RequesterBase</span>&lt;<span class="type">ClassA</span>&gt; = <span class="keyword">new</span> <span class="type">RequesterForServerA</span>(<span class="symbol">'server</span> <span class="type">A</span>');</span><br><span class="line">let requesterB: <span class="type">RequesterBase</span>&lt;<span class="type">ClassB</span>&gt; = <span class="keyword">new</span> <span class="type">RequesterForServerB</span>(<span class="symbol">'server</span> <span class="type">B</span>');</span><br><span class="line"></span><br><span class="line">requesterA.reqeustData();</span><br><span class="line">requesterB.reqeustData();</span><br><span class="line"></span><br><span class="line"><span class="comment">//输出</span></span><br><span class="line">send request, url: server <span class="type">A</span></span><br><span class="line">handle response <span class="keyword">for</span> <span class="type">Server</span> <span class="type">A</span></span><br><span class="line"></span><br><span class="line">send request, url: server <span class="type">B</span></span><br><span class="line">handle response <span class="keyword">for</span> <span class="type">Server</span> <span class="type">B</span></span><br></pre></td></tr></table></figure></p>
<p>这里可以看到主体功能由基类RequesterBase实现，两个子类则实现解析数据这些细节，这样就达到了消除重复代码的目的。<br>如果还有个ServerC的request发送部分也不一样，也没关系，TypeScript天生虚函数，在子类直接实现reqeustData即可，多态的作用下，运行时还是会调用到子类上。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/设计模式/" rel="tag"># 设计模式</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2018/09/23/TypeScript-学习笔记-之-枚举与类型推断/" rel="next" title="TypeScript 学习笔记 之 枚举与类型推断">
                <i class="fa fa-chevron-left"></i> TypeScript 学习笔记 之 枚举与类型推断
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2018/09/23/前端性能优化常用技术手段-JavaScript-面向切面编程-AOP-入门/" rel="prev" title="前端性能优化常用技术手段--JavaScript 面向切面编程(AOP)入门">
                前端性能优化常用技术手段--JavaScript 面向切面编程(AOP)入门 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">lhweb</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">39</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">25</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/blog/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#策略模式-Strategy"><span class="nav-number">1.</span> <span class="nav-text">策略模式 Strategy</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#模板方法模式-Template-Method"><span class="nav-number">2.</span> <span class="nav-text">模板方法模式 Template Method</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lhweb</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
